import React from 'react';
import { connect } from 'dva';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { router } from 'umi';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';
import getSys from './judgeSys';

@connect(({ publicdata, loading, login }) => ({
  uploading: loading.effects['publicdata/uploadFile'],
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  login,
}))
@Form.create({})
class Batch extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      currentStep: 0,
      resultInfo: {
        // 结果数据必传
        batchName: '', // 任务名称
        taskCount: 0, // 导入数据
        successCount: 0, // 成功数据
        errorCount: 0, // 失败数据
        errorUrl: '/osApi/annualAdj/accoDeclareErrorByOs/downFile', // 下载错误信息地址
        errorParams: null, // 下载错误数据参数
      },
    };
  }

  componentDidMount() {
    const { location } = this.props;
    const { yearAdjTskId, accoId } = location.query;
    this.setState({
      yearAdjTskId,
      accoId,
    });
  }

  // 完成
  finishClick = () => {
    const { location, dispatch, login } = this.props;
    // 判断权限
    const { systemList, baseUser } = login;
    const { yearAdjTskId } = this.state;
    const userType = getSys(systemList, baseUser, location);

    const inParam = {
      yearAdjTskId,
      payWay: '0',
      state: '2',
      pageArg: { currentPage: 1, pageSize: 10 },
    };

    dispatch({
      type: 'annualbasic/fetchImportDeclList',
      payload: inParam,
      callback: data => {
        // 如果有未完成的，则跳转到二级列表，如果没有，则跳转到一级列表
        if (data.totalCount) {
          router.goBack();
        } else if (String(userType) === 'independence') {
          // 自主
          router.push('/more/localannualbasic/index?activeKey=4');
        } else {
          router.push('/more/annualbasicadjust/index?activeKey=4');
        }
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    this.setState({ currentStep });
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch } = this.props;
    const { yearAdjTskId, accoId } = this.state;
    const uploadQuery = { yearAdjTskId, accoId };
    const { currentStep, resultInfo } = this.state;
    // 需要携带参数即传：batchQuery，不需要传参可省略
    Object.entries(uploadQuery).forEach(([key, value]) => {
      formData.append(key, value);
    });
    dispatch({
      type: 'publicdata/uploadFile',
      uploadUrl: '/osApi/annualAdj/accoDeclareByOs/upload',
      payload: formData,
      callback: data => {
        // 可选择性操作，需要上传进度必须有调接口，不需要可直接去看结果
        this.setState({
          resultInfo: {
            ...resultInfo,
            ...data,
            errorParams: { errorDataBatchId: data.errorDataBatchId, yearAdjTskId, accoId },
            batchName: '批量申报结果导入',
          },
          currentStep: currentStep + 1,
        });
      },
    });
  };

  render() {
    const { uploading } = this.props;
    const { currentStep, resultInfo, yearAdjTskId, accoId } = this.state;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: {
                yearAdjTskId,
                accoId,
              },
              url: '/osApi/annualAdj/accoDeclareHeadByOs/downFile',
              type: 'POST',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
          />
          <Finish
            title="完成"
            resultInfo={resultInfo}
            setCurrentstep={this.setCurrentstep}
            finishClick={this.finishClick}
          />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}

export default Batch;
